﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using NUnit.Framework;
using WPFCABShell.Shell.Services;
using SCSFContrib.CompositeUI.WPF.UIElements;

namespace WPFCABShell.UnitTest.Shell.Services
{
	[TestFixture]
	public sealed class GroupingGroupingItemsControlUIAdapterFactoryTest : UnitTest.Shared.UnitTest
	{
		private GroupingItemsControlUIAdapterFactory _groupingGroupingItemsControlUIAdapterFactory;

		protected override void SetUpCore()
		{
			base.SetUpCore();
			_groupingGroupingItemsControlUIAdapterFactory = new GroupingItemsControlUIAdapterFactory();
		}

		[Test]
		public void Supports_ShouldOnlyReturnTrueForItemsControlsAndDependencyObjectsHostedInItemsControls()
		{
			Assert.IsFalse(_groupingGroupingItemsControlUIAdapterFactory.Supports(null));
			Assert.IsFalse(_groupingGroupingItemsControlUIAdapterFactory.Supports(new Button()));
			Assert.IsFalse(_groupingGroupingItemsControlUIAdapterFactory.Supports(new TextBox()));
			Assert.IsFalse(_groupingGroupingItemsControlUIAdapterFactory.Supports(new TextBlock()));
			Assert.IsFalse(_groupingGroupingItemsControlUIAdapterFactory.Supports("abc"));
			Assert.IsTrue(_groupingGroupingItemsControlUIAdapterFactory.Supports(new ItemsControl()));
			Assert.IsTrue(_groupingGroupingItemsControlUIAdapterFactory.Supports(new ListBox()));
			Assert.IsTrue(_groupingGroupingItemsControlUIAdapterFactory.Supports(new TreeView()));

			ItemsControl itemsControl = new ItemsControl();
			string s = "hello";
			MenuItem menuItem = new MenuItem();
			Hyperlink hyperlink = new Hyperlink();
			itemsControl.Items.Add(s);
			itemsControl.Items.Add(menuItem);
			itemsControl.Items.Add(hyperlink);
			Assert.IsFalse(_groupingGroupingItemsControlUIAdapterFactory.Supports(s));
			Assert.IsTrue(_groupingGroupingItemsControlUIAdapterFactory.Supports(menuItem));
			Assert.IsTrue(_groupingGroupingItemsControlUIAdapterFactory.Supports(hyperlink));
		}

		[Test]
		[ExpectedException(typeof(ArgumentException), ExpectedMessage = "uiElement is not an ItemsControl and is not hosted inside an ItemsControl.\r\nParameter name: uiElement")]
		public void GetAdapter_ShouldThrowIfItemIsNull()
		{
			_groupingGroupingItemsControlUIAdapterFactory.GetAdapter(null);
		}

		[Test]
		[ExpectedException(typeof(ArgumentException), ExpectedMessage = "uiElement is not an ItemsControl and is not hosted inside an ItemsControl.\r\nParameter name: uiElement")]
		public void GetAdapter_ShouldThrowIfNotAnItemsControlAndIsNotHostedInItemsControl()
		{
			_groupingGroupingItemsControlUIAdapterFactory.GetAdapter(new Button());
		}

		[Test]
		[ExpectedException(typeof(ArgumentException), ExpectedMessage = "uiElement is not an ItemsControl and is not hosted inside an ItemsControl.\r\nParameter name: uiElement")]
		public void GetAdapter_ShouldThrowIfNotAnItemsControlAndNotADependencyObjectHostedInItemsControl()
		{
			ItemsControl itemsControl = new ItemsControl();
			object o = new object();
			itemsControl.Items.Add(o);
			_groupingGroupingItemsControlUIAdapterFactory.GetAdapter(o);
		}

		[Test]
		public void GetAdapter_ShouldGetAdapterForItemsControls()
		{
			Assert.IsInstanceOfType(typeof(GroupingItemsControlUIAdapter), _groupingGroupingItemsControlUIAdapterFactory.GetAdapter(new ItemsControl()));
			Assert.IsInstanceOfType(typeof(GroupingItemsControlUIAdapter), _groupingGroupingItemsControlUIAdapterFactory.GetAdapter(new MenuItem()));
			Assert.IsInstanceOfType(typeof(GroupingItemsControlUIAdapter), _groupingGroupingItemsControlUIAdapterFactory.GetAdapter(new ListBox()));
			Assert.IsInstanceOfType(typeof(GroupingItemsControlUIAdapter), _groupingGroupingItemsControlUIAdapterFactory.GetAdapter(new TreeView()));
		}

		[Test]
		public void GetAdapter_ShouldGetAdapterForDependencyObjectsHostedInItemsControls()
		{
			ItemsControl itemsControl = new ItemsControl();
			Button button = new Button();
			Separator separator = new Separator();
			Hyperlink hyperlink = new Hyperlink();
			itemsControl.Items.Add(button);
			itemsControl.Items.Add(separator);
			itemsControl.Items.Add(hyperlink);

			//may need a GroupingItemsControlChildUIAdapter at some stage
			Assert.IsInstanceOfType(typeof(ItemsControlChildUIAdapter), _groupingGroupingItemsControlUIAdapterFactory.GetAdapter(button));
			Assert.IsInstanceOfType(typeof(ItemsControlChildUIAdapter), _groupingGroupingItemsControlUIAdapterFactory.GetAdapter(separator));
			Assert.IsInstanceOfType(typeof(ItemsControlChildUIAdapter), _groupingGroupingItemsControlUIAdapterFactory.GetAdapter(hyperlink));
		}
	}
}
